package com.itestor.iptv.modules.common.actions;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.common.service.GroupManageService;
import com.itestor.iptv.modules.common.utils.CommonConstant;
import com.itestor.iptv.modules.common.vo.GroupManageOBJ;

/**
 * 
 * @author Mr.li
 * 
 */
public class GroupManageNaviAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(GroupManageNaviAction.class);

    private GroupManageService groupManageService;

    /**
     * 添加虚拟组
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {       
        return mapping.findForward(CommonConstant.FORWARD_GROUP_ADD);
    }

    /**
     * 修改虚拟组
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        GroupManageOBJ group = groupManageService.get(Integer.parseInt(id));
        if (group == null) {
            return null;
        }

        LoggerUtils.logActionInfo("To modify the group " + group + ".",
                logger, request);

        request.setAttribute(CommonConstant.REQUEST_ATTR_GROUP, group);
        
        return mapping.findForward(CommonConstant.FORWARD_GROUP_MODIFY);
    }

    /**
     * 分配设备
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward addRelRobot(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String groupManageId = request.getParameter("groupManageId");
        String robotIds = request.getParameter("robotIds");
        
        logger.info("distributeRelRobot IdGroup :" +groupManageId +"  robotIds :"+robotIds);
        
        if (StringUtils.isEmpty(groupManageId)) {
            return null;
        }
        
        String [] ids=robotIds.split(",");        
        List<Integer> robotIdList=new ArrayList<Integer>();
        
        for(String id : ids){
            robotIdList.add(Integer.parseInt(id));
        }
        
        //分配设备
        groupManageService.distributeRobot(Integer.parseInt(groupManageId),robotIdList);    
        
        request.setAttribute("queryIdGroup", groupManageId);
        
        return mapping.findForward(CommonConstant.FORWARD_GROUP_ADDROBOT);
    }

    
    /**
     * 删除分组设备
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward deleteRelRobot(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String groupManageId = request.getParameter("groupManageId");
        
        if (StringUtils.isEmpty(groupManageId)) {
            return null;
        }
        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        
        if (null != pvids && pvids.length > 0) {            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            LoggerUtils.logActionInfo("Delete the GroupManage - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            groupManageService.deleteRelRobot(groupManageId,ids);
        }
                
        request.setAttribute("queryIdGroup", groupManageId);       
        
        return mapping.findForward(CommonConstant.FORWARD_GROUP_DELROBOT);
    }
    
    /**
     * 删除虚拟组
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        if (null != pvids && pvids.length > 0) {
            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            LoggerUtils.logActionInfo("Delete the GroupManage - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);

            groupManageService.delete(ids);
        }
        
        return mapping.findForward(CommonConstant.FORWARD_GROUP_DELETE);
    }

    /** 
     * @param pGroupManageService
     */
    public void setGroupManageService(GroupManageService pGroupManageService) {
        groupManageService = pGroupManageService;
    }

}
